﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;

namespace MyWebPagesStarterKit
{
    /// <summary>
    /// Summary description for ImageGallery
    /// </summary>
    public class ImageGallery : Section<ImageGallery.Data>
    {
        public ImageGallery(string id) : base(id) { }

        public ImageGallery()
        {
            _data.Entries = new DataSet();
            _data.Entries.Tables.Add(new DataTable("Entries"));

            DataColumn primaryKeyColumn = new DataColumn("Guid", typeof(Guid));
            primaryKeyColumn.Unique = true;

            _data.Entries.Tables["Entries"].Columns.Add(primaryKeyColumn);
            _data.Entries.Tables["Entries"].PrimaryKey = new DataColumn[] { primaryKeyColumn };

            _data.Entries.Tables["Entries"].Columns.AddRange(
                new DataColumn[] 
                {
                    new DataColumn("Title", typeof(string)),
                    new DataColumn("ImageEntry", typeof(ImageEntry))
                }
           );
        }

        public DataTable Entries
        {
            get { return _data.Entries.Tables["Entries"]; }
        }

        public DataRow GetRow(string id)
        {
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("Guid = '{0}'", id));
            if (foundRows.Length > 0)
                row = foundRows[0];
            return row;
        }

        public ImageEntry GetImageEntry(string id)
        {
            ImageEntry entry = null;
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("Guid = '{0}'", id));
            if (foundRows.Length > 0)
            {
                row = foundRows[0];
                entry = (ImageEntry)row["ImageEntry"];
            }
            return entry;
        }

        public List<ImageEntry> GetImageEntries()
        {
            List<ImageEntry> entries = new List<ImageEntry>();
            if (Entries!=null)
            {
                foreach (DataRow row in Entries.Rows)
                    entries.Add((ImageEntry)row["ImageEntry"]);
            }
            return entries;
        }

        public override List<SearchResult> Search(string searchString, WebPage page)
        {
            List<SearchResult> list = new List<SearchResult>();
            return list;
        }

        public string Upload(FileUpload image)
        {
            string serverPath = HttpContext.Current.Server.MapPath("~/App_Data/ImageGallery/Images");
            if (!Directory.Exists(serverPath))
                Directory.CreateDirectory(serverPath);

            string fileName = image.PostedFile.FileName;
            string path = HttpContext.Current.Server.MapPath(
                        string.Format("~/App_Data/ImageGallery/Images/{0}",
                        Path.GetFileName(fileName))
                    );
            image.SaveAs(path);
            return fileName;
        }

        public override Position Position
        {
            get
            {
                return _data.Position;
            }
            set
            {
                _data.Position = value;
            }
        }

        public override bool IsOnline
        {
            get
            {
                return _data.IsOnline;
            }
            set
            {
                _data.IsOnline = value;
            }
        }

        public string Title
        {
            get { return _data.Title; }
            set { _data.Title = value; }
        }

        public class Data
        {
            public bool IsOnline = false;
            public Position Position;
            public DataSet Entries;
            public string Title = string.Empty;
        }

        public class ImageEntry
        {
            public ImageEntry(){}

            private string _InternalTitle = string.Empty;
            private string _Description = string.Empty;
            private string _ImagePath = string.Empty;
            private string _ImageAlternateImagePath = string.Empty;

            public string InternalTitle
            {
                get { return _InternalTitle; }
                set { this._InternalTitle = value; }
            }

            public string Description
            {
                get { return this._Description; }
                set { this._Description = value; }
            }

            public string ImagePath
            {
                get {return this._ImagePath; }
                set { this._ImagePath = value; }
            }

            public string ImageAlternateImagePath
            {
                get { return _ImageAlternateImagePath; }
                set { this._ImageAlternateImagePath = value; }
            }
        }

       
    }
}
